#include <Arduino.h>
#include <Wire.h>
#include "Freenove_WS2812_Lib_for_ESP32.h"
#include <WiFi.h>

#define LEDS_COUNT  32
#define LEDS_PIN	4
#define CHANNEL		0

#define LED_BUILTIN   02


const char *ssid = "fei";//WIFI   2076
const char *password = "18832825018yf";//WIFI密码  0123456789

Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);

u8 m_color[5][3] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}, {0, 0, 0} };
int delayval = 2;
u8 r_value = 0, g_value = 255, b_value = 0, test_value = 0;

int8_t right = 0, down = 0, ok = 0, clear = 0, randomDraw = 0;
int8_t right_num = 0, right_sub_num = 0, down_num = 0, ok_num = 0, clear_num = 0, randomDraw_num = 0;
int8_t right_flag = 0, down_flag = 0, ok_flag = 0, clear_flag = 0, randomDraw_flag = 0;

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 8*3600;//这里采用UTC计时，中国为东八区，就是 8*60*60
const int   daylightOffset_sec = 0;//同上 

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

void led_all(void)
{
    for (int i = 0; i < LEDS_COUNT; i++) {
      strip.setLedColorData(i, r_value, g_value, b_value);
      strip.show();
      delay(delayval);
    }
}

void setup(void)
{
    strip.begin();
	  strip.setBrightness(150);

    //USART
    Serial.begin(115200);
   
    pinMode(LED_BUILTIN, OUTPUT);

    Serial.print("START");
 
    WiFi.begin(ssid, password);//连接到网络
 
    while (WiFi.status() != WL_CONNECTED) {//等待网络连接成功
        delay(500);
        Serial.print(".");
    }
 
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());//打印模块IP

    //init and get the time
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    printLocalTime();
}

void loop(void)
{
    //LED
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
    delay(200);
    //delay(500);

  //////////////////////////time///////////////////      
    struct tm timeinfo;
    if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    }

    //早晨全绿  6：00  //每3分钟变一个
    if((timeinfo.tm_hour >= 6) && (timeinfo.tm_hour <= 10)) //6:00- 10:00
    {
      test_value = (timeinfo.tm_hour-6)*20 + (timeinfo.tm_min/3);
    }
    else if((timeinfo.tm_min <= 40) && (timeinfo.tm_hour == 11)) //11:00- 11:40
    {
      test_value = 113 + (timeinfo.tm_min/3); //113 + 40/3  
    }
    else if((timeinfo.tm_min > 40) && (timeinfo.tm_hour == 11))  //11:40- 12:00
    {
      test_value = 128 + (timeinfo.tm_min/3); 
    }

    if((timeinfo.tm_hour == 6) && (timeinfo.tm_min == 0))  //6:00
    {
      test_value = 0;
    }
    

    //中午半绿半红 11：40  128 128

    if((timeinfo.tm_hour == 11) && (timeinfo.tm_min == 40)) //11:40
    {
      test_value = 128;
    }

    if((timeinfo.tm_hour >= 12) && (timeinfo.tm_hour <= 20)) ////12:00- 20:00
    {
      test_value = 128 + (timeinfo.tm_hour-12)*13 + (timeinfo.tm_min/3);
    }
    else if((timeinfo.tm_min <= 30) && (timeinfo.tm_hour == 21)) //21:00- 21:30
    {
      test_value = 124 + (timeinfo.tm_min/10); //255 + 30/3
    }
   

    

    //晚上全红 21：30  //每5分钟一个
    if((timeinfo.tm_hour == 21) && (timeinfo.tm_min == 30))//21:30
    {
      test_value = 255;
    }
    //凌晨切换
    if(timeinfo.tm_hour == 24)//24:00
    {
      test_value = 0;
    }

    r_value = test_value;
		g_value = 255 - test_value;
    //test 
    // test_value = (test_value + 1)%255;
    // r_value = test_value;
		// g_value = 255 - test_value;

  //////////////////////////ws2812///////////////////  
    led_all();
   
}
